/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.longview.io.modules.grasscoverage.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.longview.io.common.config.Global;
import com.longview.io.common.persistence.Page;
import com.longview.io.common.web.BaseController;
import com.longview.io.common.utils.StringUtils;
import com.longview.io.modules.grasscoverage.entity.GrassCoverage;
import com.longview.io.modules.grasscoverage.service.GrassCoverageService;

/**
 * 草场盖度Controller
 * @author zf
 * @version 2018-05-18
 */
@Controller
@RequestMapping(value = "${adminPath}/grasscoverage/grassCoverage")
public class GrassCoverageController extends BaseController {

	@Autowired
	private GrassCoverageService grassCoverageService;
	
	@ModelAttribute
	public GrassCoverage get(@RequestParam(required=false) String id) {
		GrassCoverage entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = grassCoverageService.get(id);
		}
		if (entity == null){
			entity = new GrassCoverage();
		}
		return entity;
	}
	
	@RequiresPermissions("grasscoverage:grassCoverage:view")
	@RequestMapping(value = {"list", ""})
	public String list(GrassCoverage grassCoverage, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<GrassCoverage> page = grassCoverageService.findPage(new Page<GrassCoverage>(request, response), grassCoverage); 
		model.addAttribute("page", page);
		return "modules/grasscoverage/grassCoverageList";
	}

	@RequiresPermissions("grasscoverage:grassCoverage:view")
	@RequestMapping(value = "form")
	public String form(GrassCoverage grassCoverage, Model model) {
		model.addAttribute("grassCoverage", grassCoverage);
		return "modules/grasscoverage/grassCoverageForm";
	}

	@RequiresPermissions("grasscoverage:grassCoverage:edit")
	@RequestMapping(value = "save")
	public String save(GrassCoverage grassCoverage, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, grassCoverage)){
			return form(grassCoverage, model);
		}
		grassCoverageService.save(grassCoverage);
		addMessage(redirectAttributes, "保存草场盖度成功");
		return "redirect:"+Global.getAdminPath()+"/grasscoverage/grassCoverage/?repage";
	}
	
	@RequiresPermissions("grasscoverage:grassCoverage:edit")
	@RequestMapping(value = "delete")
	public String delete(GrassCoverage grassCoverage, RedirectAttributes redirectAttributes) {
		grassCoverageService.delete(grassCoverage);
		addMessage(redirectAttributes, "删除草场盖度成功");
		return "redirect:"+Global.getAdminPath()+"/grasscoverage/grassCoverage/?repage";
	}

	@RequestMapping(value = "deletebyid")
	public String deletebyid(GrassCoverage grassCoverage,HttpServletResponse response) {
		grassCoverageService.delete(grassCoverage);
		grassCoverage.setCode("0000");
		grassCoverage.setMessage("成功");
		return renderString(response, grassCoverage);
	}
}